package com.tianji.learning.mq;

import com.tianji.api.dto.trade.OrderBasicDTO;
import com.tianji.common.constants.MqConstants;
import com.tianji.common.utils.CollUtils;
import com.tianji.learning.service.ILearningLessonService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Description
 * @Author lyx
 * @since 2025/1/16 16:56
 */

@Component
@Slf4j
@RequiredArgsConstructor//使用构造器
public class LessonChangeListerner {


    final ILearningLessonService lessonService;

    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = "learning.lesson.pay.queue",durable = "true"),
        exchange = @Exchange(value = MqConstants.Exchange.ORDER_EXCHANGE,type = ExchangeTypes.TOPIC),
        key = MqConstants.Key.ORDER_PAY_KEY))
    public void onMsg(OrderBasicDTO dto){
        log.info("LessonChangeListener 接收到消息 用户{}，添加课程{}",dto.getUserId(),dto.getCourseIds());
        //1.校验
        if (dto.getUserId() == null
                || dto.getOrderId() == null
                || dto.getCourseIds() == null
                || CollUtils.isEmpty(dto.getCourseIds())){

            return;
        }

        //2.调用service
        lessonService.addUserLesson(dto.getUserId(),dto.getCourseIds());
    }
}
